package umg.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.*;

import umg.util.DBUtil;
import umg.util.PropertyManager;
import umg.util.StringUtils;

public class RollOverJTable extends JTable
{
	private static final long serialVersionUID = 4068997191384468163L;
	private int rollOverRowIndex = -1;
	private static DBUtil db = null;
	static String lastSearch = "";

	public RollOverJTable(TableModel model)
	{
		super(model);
		RollOverListener lst = new RollOverListener();
		addMouseMotionListener(lst);
		addMouseListener(lst);
		this.setSelectionBackground(Color.BLACK);
		this.setSelectionForeground(Color.RED);
	}

	Component comp;

	//TODO: why is this slow
	public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
	{
		try
		{
			comp = super.prepareRenderer(renderer, row, column);
		}
		catch (Exception e)
		{
			System.out.println("Exception: " + e.getMessage());
			if (PropertyManager.isDebug()) e.printStackTrace();////ignore
			// java.lang.IndexOutOfBoundsException: Invalid index
			return null;
		}
		if (comp instanceof DefaultTableCellRenderer)
		{
			DefaultTableCellRenderer c = (DefaultTableCellRenderer) comp;
			if (!lastSearch.equals(c.getText()) && isRowSelected(row) /*&& row == rollOverRowIndex */&& column == 0 && isColumnSelected(column))//name
			{
				if (db == null) db = new DBUtil();
				// user rolled over a name and single clicked on it.
				// System.out.println(c.getText());
				GUI.setSearchResultsTextAreaText(db.searchByName("%" + StringUtils.oldReplaceAll(c.getText(), "'", "''") + "%"));
				// ListSelectionModel selModel = this.getSelectionModel();
				// selModel.clearSelection();
				GUI.setSearchResultsTextAreaCaretPosition(0);
				GUI.searchAndLogsTabbedPane.setSelectedIndex(1);// move to tab 0
				GUI.logToStatus("Searched for Name \"" + c.getText() + "\"");
				// c.setToolTipText(db.searchByName("%" +
				// StringUtils.oldReplaceAll(c.getText(), "'", "''") + "%"));
				// GUI.searchResultsTextArea.setCaretPosition(0);
				lastSearch = c.getText();
			}
			else if (!lastSearch.equals(c.getText()) && isRowSelected(row) /*&& row == rollOverRowIndex*/&& column == 4 && isColumnSelected(column))//ip
			{
				if (db == null) db = new DBUtil();
				// user rolled over an IP and single clicked on it.
				// System.out.println(c.getText());
				String subnet=StringUtils.getSubnet(c.getText());

				GUI.setSearchResultsTextAreaText(db.searchByIP(subnet + "%"));
				// ListSelectionModel selModel = this.getSelectionModel();
				// selModel.clearSelection();
				GUI.setSearchResultsTextAreaCaretPosition(0);
				GUI.searchAndLogsTabbedPane.setSelectedIndex(1);
				GUI.logToStatus("Searched for IP \"" + subnet  + "\"");
				lastSearch = c.getText();
			}
		}//if DefaultTableCellRenderer
		return comp;
	}

	private class RollOverListener extends MouseInputAdapter
	{

		public void mouseExited(MouseEvent e)
		{
			rollOverRowIndex = -1;
			repaint();
		}

		public void mouseMoved(MouseEvent e)
		{
			int row = rowAtPoint(e.getPoint());
			if (row != rollOverRowIndex)
			{
				rollOverRowIndex = row;
				repaint();
			}
		}
	}

	// Returning the Class of each column will allow different renderers to be
	// used based on Class
	public Class<?> getColumnClass(int column)
	{
		//if(column==1||column==4) return ImageIcon.class;
		//System.out.println("in getColumnClass(). col="+column);
		if (column == 1)
		{
			//System.out.println("type=ImageIcon.class");
			return Icon.class;
		}
		return String.class;
	}
}
